program mkrawnt
  use suty
  use kuty
  implicit none
  interface 
     subroutine decs(e,xyz,etc)
       integer*8,intent(in)::e
       integer*4,intent(out)::xyz(3)
       integer*4,intent(out)::etc(4)
     end subroutine decs
  end interface      
  real,parameter::dx=2 ! mm
  real,parameter::dy=2
  real,parameter::dz=2 ! <<< PHONY
  !
  integer::nd=8   ! number of detectors
  integer::nx=512 ! 
  integer::ny=512
  !
  integer*4,target::xyz(3),etc(4)
  
  integer*4,allocatable::buf4(:)
  character*512 fin,fout,str
  integer*4 istat
  integer*8 n,ie,b1,b2,nb
  integer*8 fs
  integer*4,pointer::ix,iy,iz
  integer jx,jy
  !
  logical lo_found
  integer nok,nlost,nnull,npair,nbtot
  integer*8 ev
  pointer(pev,ev)
  integer ifile,nf
  integer istep
  integer pmtid
  integer narg,len
  integer iu

  !
  narg=command_argument_count()
  if(narg<4) then
     call usage
     call exit(1)
  end if

  call get_command_argument(1,str)
  nx=a2i(str)
  if(nx<=0) call claimer("*** Invalid nx: "//trim(str))

  call get_command_argument(2,str)
  ny=a2i(str)
  if(ny<=0) call claimer("*** Invalid ny: "//trim(str))

  call get_command_argument(3,str)
  nd=a2i(str)
  if(nd<=0) call claimer("*** Invalid nd: "//trim(str))

  call get_command_argument(4,fout,len)

  nf=narg-4

  ix=>xyz(1)
  iy=>xyz(2)
  iz=>xyz(3)

  ifile=0
  istep=0
  nbtot=0
  nok=0
  nlost=0
  nnull=0
  npair=0

  iu=0
  if(fout/="") then
     if(trim(fout)/="-") then
        open(unit=10,file=fout,status="replace",iostat=istat)
!!$        open(unit=10,file=fout,status="new",iostat=istat)
        if(istat==0) then
           iu=10
        else
           call claimer("*** cannot open file: "//trim(fout))
!!$           call claimer("*** cannot open file as new: "//trim(fout))
        end if
     end if
  end if
  
  file: do ifile=5,narg
     call get_command_argument(ifile,fin)
     write(*,*) "Opening file: "//trim(fin)
     flush(6)
     open(unit=1,file=fin,status="old",access="stream",iostat=istat)
     if(istat/=0) call claimer("*** cannot open file: "//trim(fin))
     fs=fsize(fin)
     n=fs/4
     if(allocated(buf4)) deallocate(buf4)
     allocate(buf4(n),stat=istat)
     if(istat/=0) call claimer("*** cannot allocate buf")
     b1=1
     nb=1024*1024*1024/16
     
     list: do ie=1,n/nb+1
        b2=min(b1+nb,n)
        read(1,iostat=istat) buf4(b1:b2)    
        nbtot=nbtot+b2-b1+1
        b1=b2+1
        if(istat/=0) call claimer("*** error during read: "//trim(fin)//", code="//trim(i2a(istat)))
     end do list
     close(1)
     
     lo_found=.false.
     event: do ie=1,n
        if(buf4(ie)==0) then
           nnull=nnull+1
           cycle
        end if
        if(lo_found) then
           if(get_flg(buf4(ie))==0) then
              ! pair found
              npair=npair+1
              lo_found=.false.
              pev=loc(buf4(ie-1))
              call decs(ev,xyz,etc)
              pmtid=etc(2)+1
              jy=iy+1
              jx=ix+1
              write(iu,"(4(x,i4))") pmtid,jx,jy,iz
              nok=nok+1
           else
              nlost=nlost+1
              lo_found=.true.
           end if
        else        
           if(get_flg(buf4(ie))==1) then
              lo_found=.true.
           else
              nlost=nlost+1
           end if
        end if
     end do event
  end do file

  call print_stat

contains

  subroutine print_stat
    write(*,*) "Number of files read:      ",nf
    write(*,*) "Number of bytes read:      ",nb*4
    write(*,*) "Number of total events:    ",npair
    write(*,*) "Number of accept events:   ",nok
    write(*,*) "Number of lost 4-bytes:    ",nlost    
  end subroutine print_stat

  integer function get_flg(e)
    integer*4,intent(in)::e
    get_flg=ishft(e,-30)
  end function get_flg
  
  subroutine claimer(msg,code)
    character*(*),intent(in),optional::msg
    integer,intent(in),optional::code
    integer rc
    if(present(msg)) write(0,"(x,a)") msg
    rc=-1
    if(present(code)) rc=code
    call exit(rc)
  end subroutine claimer

  subroutine usage
    write(*,100)
100 format(x, "Usage: mkrawnt nx ny nd output infiles...")
  end subroutine usage

end program mkrawnt
